<!--
 * @Descripttion: 
 * @version: 
 * @Author: 彭勃
 * @Date: 2023-03-29 13:31:49
 * @LastEditors: sueRimn
 * @LastEditTime: 2023-04-06 15:18:39
-->
<template>
  <EnteringForm
    ref="enteringForm"
    v-bind="dialog"
		width="60%"
    @close="close"
    @submit="submit"
  >
    <el-form ref="formRef" :model="queryParams" :rules="rules" label-width="140px">
      <el-row type="flex" style="flex-wrap: wrap">
				<el-col :xs="24" :sm="12" :lg="12">
          <el-form-item label="标题:" prop="name">
            <el-input
              v-model.trim="queryParams.name"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="12">
              <el-form-item label="类型" prop="location">
                <el-select
                  v-model="queryParams.location"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option label="首页头部" value="INDEX_TOP"/>
                  <el-option label="首页底部" value="INDEX_BOTTOM"/>
                  <el-option label="首页顶部" value="INTEGRAL_TOP"/>
                  <el-option label="关于我们" value="ABOUT_US"/>
                  <el-option label="我的" value="MY"/>
                  <el-option label="平台介绍" value="PLATFORM"/>
                  <el-option label="商学院" value="STUDY"/>
                </el-select>
              </el-form-item>
            </el-col>
       
        <el-col :xs="24" :sm="24" :lg="24">
          <el-form-item label="封面图:" prop="cover">
            <ImageUpload
              v-model="queryParams.cover"
              :limit="1"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :lg="24">
          <el-form-item label="内容图:" prop="images">
            <ImageUpload
              v-model="queryParams.images"
              :limit="6"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </EnteringForm>
</template>

<script setup>
import { getCurrentInstance, reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { listPage, add, edit, del } from '@/api/operates/banner/index'

const { proxy } = getCurrentInstance()
const emit = defineEmits(["success"]);
const dialogType = ref('')
const dialog = reactive({
  visible: false,
  title: "",
});
const rules = reactive({
  name: [{ required: true, message: "请输入", trigger: "blur" }],
  location: [{ required: true, message: "请选择", trigger: "change" }],
  cover: [{ required: true, message: "请上传", trigger: "change" }],
  images: [{ required: true, message: "请上传", trigger: "change" }],
});
const queryParams = ref({})

function init(type, record){
	dialogType.value = type
	dialogType.value=='add' ? dialog.title='新增' : dialog.title='编辑'
	dialog.visible = true
	// 如果是编辑，回显数据
	if(dialogType.value=='edit'){
		queryParams.value = { ...record };
	}
};
function close(){
  dialog.visible = false;
  proxy.$refs.formRef.resetFields();
	queryParams.value = {}
};
function submit() {
  proxy.$refs.formRef.validate((valid) => {
    if (valid) {
      let query = {
        ...queryParams.value, 
      }
      if(dialogType.value=='add'){
				add(query).then((res) => {
					if (res.code == 200) {
						ElMessage.success("新增成功");
						close();
						emit("success",'add');
					} else {
						ElMessage.error(res.message || "新增失败");
					}
				});
			} else {
				edit(query).then((res) => {
					if (res.code == 200) {
						ElMessage.success("编辑成功");
						close();
						emit("success",'edit');
					} else {
						ElMessage.error(res.message || "编辑失败");
					}
				});
			}
    }
  });
}
defineExpose({ init });
</script>

<style scoped>
.v-enter-active,
.v-leave-active {
  transition: opacity 0.5s ease;
}

.v-enter-from,
.v-leave-to {
  opacity: 0;
}
</style>
